﻿#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QPushButton>
#include <QScreen>
#include <QPixmap>
#include <QFileDialog>
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>

QtWidgetsApplication1::QtWidgetsApplication1(QWidget* parent)
    : QMainWindow(parent), isSelecting(false), isFullScreen(false)
{
    // 设置窗口无边框
    setWindowFlags(Qt::FramelessWindowHint);

    // 创建按钮
    screenshotButton = new QPushButton("截图", this);
    // 连接按钮点击事件到槽函数
    connect(screenshotButton, &QPushButton::clicked, this, &QtWidgetsApplication1::onScreenshotButtonClicked);

    // 初始化 QRubberBand
    rubberBand = new QRubberBand(QRubberBand::Rectangle, this);

    // 设置窗口的布局，将按钮放置在布局中心
    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(screenshotButton, 0, Qt::AlignCenter);
    QWidget* centralWidget = new QWidget();
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
}

QtWidgetsApplication1::~QtWidgetsApplication1()
{
}

void QtWidgetsApplication1::onScreenshotButtonClicked()
{
    // 隐藏按钮，开始选择截图区域
    screenshotButton->hide();
    isSelecting = true;
    setCursor(Qt::CrossCursor);

    // 隐藏主窗口
    hide();

    // 使用 QTimer 延迟1秒
    QTimer::singleShot(10, this, &QtWidgetsApplication1::captureScreenAndShowFullScreen);
}

void QtWidgetsApplication1::captureScreenAndShowFullScreen()
{
    // 捕获当前屏幕的截图
    QScreen* screen = QGuiApplication::primaryScreen();
    QPixmap originalPixmap = screen->grabWindow(0);

    // 设置背景
    backgroundPixmap = originalPixmap;
    update();

    // 全屏显示窗口
    showFullScreen();
    isFullScreen = true;
}

void QtWidgetsApplication1::mousePressEvent(QMouseEvent* event)
{
    if (isSelecting) {
        origin = event->pos();
        rubberBand->setGeometry(QRect(origin, QSize()));
        rubberBand->show();
    }
}

void QtWidgetsApplication1::mouseMoveEvent(QMouseEvent* event)
{
    if (isSelecting) {
        rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
    }
}

void QtWidgetsApplication1::mouseReleaseEvent(QMouseEvent* event)
{
    if (isSelecting) {
        isSelecting = false;
        setCursor(Qt::ArrowCursor);

        // 获取选择的区域
        QRect selectedArea = rubberBand->geometry();
        rubberBand->hide();

        // 截取选择的区域
        QPixmap croppedPixmap = backgroundPixmap.copy(selectedArea);

        // 保存截图到文件
        QString fileName = QFileDialog::getSaveFileName(this, "保存截图", "", "Images (*.png *.xpm *.jpg)");
        if (!fileName.isEmpty()) {
            croppedPixmap.save(fileName);
        }

        // 清除背景
        backgroundPixmap = QPixmap();
        update();

        // 退出全屏模式
        if (isFullScreen) {
            showNormal();
            isFullScreen = false;
        }

        // 重新显示主窗口
        show();

        // 重新显示按钮并确保其在中心位置
        QRect geometry = this->geometry();
        int buttonWidth = screenshotButton->width();
        int buttonHeight = screenshotButton->height();
        QVBoxLayout* layout = static_cast<QVBoxLayout*>(centralWidget()->layout());
        layout->addWidget(screenshotButton, 0, Qt::AlignCenter);
        screenshotButton->show();
    }
}

void QtWidgetsApplication1::paintEvent(QPaintEvent* event)
{
    if (!backgroundPixmap.isNull()) {
        QPainter painter(this);
        painter.drawPixmap(0, 0, backgroundPixmap);
    }
}